// detail/comment/comment.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  //编辑评论
  setcomment:function(e){
    //编辑时间、内容
    var comment={
      commenttext:e.detail.value,
      sayid:this.data.sayid,
      userid:this.data.id,
      username:this.data.name,
      userimg:this.data.img
    }
    //存储当条评论
    wx.setStorageSync('comment',comment)
  },

   //发布评论
   addComment:function(){
    //获取需发布评论
    let comment=wx.getStorageSync('comment')
    //获取发布时间
    var util=require('../../pages/utils/util.js')
    comment.commenttime=util.formatTime(new Date())

    //添加评论
    wx.request({
      url: `https://www.prebeauty.work:3001/api/comment`,
      data:{
        userid:comment.userid,
        sayid:comment.sayid,  
        commenttext:comment.commenttext,
        commenttime:comment.commenttime
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success:async (res)=>{  
        comment.commentid=res.data

        await new Promise((rv,rj) => {
          wx.request({
            url: `https://www.prebeauty.work:3001/api/says/${comment.sayid}`,
            data:{
              commentnum:++this.data.commentnum
            },
            header: {
              'content-type': 'application/json'
            },
            method: 'PUT',
            success:(res)=>{
              console.log(res.data)
              console.log("comment add success!")

              //本地添加评论
              let comments=this.data.commentlist
              comments.push(comment)
              this.setData({
                  commentlist:comments,
                  inputValue:''
              })

              console.log(this.data.say)
              console.log(this.data.commentlist)
              rv()
            },
            fail : err => {
              rv()
            }
          })
        })
      }
    })

    
  },

  //生命周期函数--监听页面加载
  onLoad: function (options) {

    this.setData({
      id: wx.getStorageSync('userid'),
      img:wx.getStorageSync('userimg'),
      name:wx.getStorageSync('username'),
    })

    const that=this

    this.setData({
      sayid:options.sayid,
      commentnum:options.commentnum
    })

    wx.request({
      url: `https://www.prebeauty.work:3001/api/comment?sayid=${options.sayid}`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:async (res)=>{
        console.log(res.data)
        let comments=res.data
        for(let i=0;i<comments.length;i++){
          // let time1=comments[i].commenttime.slice(0,10)
          // let time2=comments[i].commenttime.slice(14,19)
          // comments[i].commenttime=time1+' '+time2

          await new Promise((rv,rj) => {
            wx.request({
              url: `https://www.prebeauty.work:3001/api/userpage?userid=${comments[i].userid}`,
              header: {
                'content-type': 'application/json'
              },
              method: 'GET',
              success:(res)=>{
                comments[i].username=res.data[0].username
                comments[i].userimg=res.data[0].userimg
                rv()
              },
              fail : err => {
                rv()
              }
            })
          })
        }
      
        that.setData({
          commentlist:comments
        });
        console.log(this.data.commentlist)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})